﻿Imports WiremetModel.Entities.OEM
Imports WiremetModel.Entities

Public Class WireCertificateReportService

    Public Function GenerateReport(ByVal wireCertificate As WireCertificate) As WireCertificateReport

        Dim wireCertificateReport As WireCertificateReport = New WireCertificateReport()


        Dim chemsSubreportData As IEnumerable(Of WireCertificateChemicalCompositionReportDataRow) = GetChemicalCompositionSubreportData(wireCertificate)
        wireCertificateReport.OpenSubreport("WireCertificateChemicalCompositionReport.rpt").SetDataSource(chemsSubreportData)

        Dim mechanicalParametersSubreportData As IEnumerable(Of WireCertificateMechanicalParametersReportDataRow) = GetMechanicalParametersSubreportData(wireCertificate).OrderBy(Function(x) x.WireId)
        wireCertificateReport.OpenSubreport("WireCertificateMechanicalParametersReport.rpt").SetDataSource(mechanicalParametersSubreportData)

        Dim packagingSubreportData As IEnumerable(Of WireCertificatePackagingReportDataRow) = GetPackagingSubreportData(wireCertificate).OrderBy(Function(x) x.Name)
        wireCertificateReport.OpenSubreport("WireCertificatePackagingReport.rpt").SetDataSource(packagingSubreportData)


        wireCertificateReport.SetParameterValue("p_report_language", wireCertificate.ReportLanguage, "WireCertificateChemicalCompositionReport.rpt")
        wireCertificateReport.SetParameterValue("p_report_language", wireCertificate.ReportLanguage, "WireCertificateMechanicalParametersReport.rpt")
        wireCertificateReport.SetParameterValue("p_report_language", wireCertificate.ReportLanguage, "WireCertificatePackagingReport.rpt")

        wireCertificateReport.SetParameterValue("p_order_number", wireCertificate.OrderNumber)
        wireCertificateReport.SetParameterValue("p_customer_order_number", wireCertificate.CustomerOrderNumber)
        wireCertificateReport.SetParameterValue("p_invoice_number", wireCertificate.InvoiceNumber)
        wireCertificateReport.SetParameterValue("p_order_date", wireCertificate.OrderDate.ToShortDateString())
        wireCertificateReport.SetParameterValue("p_gatunek_name", wireCertificate.GatunekName)
        wireCertificateReport.SetParameterValue("p_wytop_name", wireCertificate.WytopName)
        wireCertificateReport.SetParameterValue("p_norma_wykonania", wireCertificate.NormaWykonania)
        wireCertificateReport.SetParameterValue("p_diameter", wireCertificate.Diameter)
        wireCertificateReport.SetParameterValue("p_tolerance_upper", wireCertificate.ToleranceUpper)
        wireCertificateReport.SetParameterValue("p_tolerance_lower", wireCertificate.ToleranceLower)
        wireCertificateReport.SetParameterValue("p_weight", wireCertificate.Weight)
        wireCertificateReport.SetParameterValue("p_spool_count", wireCertificate.SpoolCount)
        wireCertificateReport.SetParameterValue("p_customer_name", wireCertificate.CustomerName)
        wireCertificateReport.SetParameterValue("p_customer_short_name", wireCertificate.CustomerShortName)
        wireCertificateReport.SetParameterValue("p_customer_address_country", wireCertificate.CustomerAddressCountry)
        wireCertificateReport.SetParameterValue("p_customer_address_zip_code", wireCertificate.CustomerAddressZipCode)
        wireCertificateReport.SetParameterValue("p_customer_address_city", wireCertificate.CustomerAddressCity)
        wireCertificateReport.SetParameterValue("p_customer_address_street", wireCertificate.CustomerAddressStreet)
        wireCertificateReport.SetParameterValue("p_customer_address_number", wireCertificate.CustomerAddressNumber)
        wireCertificateReport.SetParameterValue("p_customer_phone_number", wireCertificate.CustomerPhoneNumber)
        wireCertificateReport.SetParameterValue("p_customer_email", wireCertificate.CustomerEmail)
        wireCertificateReport.SetParameterValue("p_customer_nip", wireCertificate.CustomerNip)
        wireCertificateReport.SetParameterValue("p_print_time", wireCertificate.PrintTime)
        wireCertificateReport.SetParameterValue("p_report_language", wireCertificate.ReportLanguage)


        Return wireCertificateReport

    End Function


    Private Function GetChemicalCompositionSubreportData(ByVal wireCertificate As WireCertificate) As IEnumerable(Of WireCertificateChemicalCompositionReportDataRow)
        Dim chemsSubreportData As New List(Of WireCertificateChemicalCompositionReportDataRow)
        For Each chem As WireCertificateChemicalComposition In wireCertificate.ChemicalComposition
            chemsSubreportData.Add(
                New WireCertificateChemicalCompositionReportDataRow With {
                    .ElementAmount = chem.ElementAmount,
                    .ElementName = chem.ElementName
                })
        Next
        Return chemsSubreportData
    End Function

    Private Function GetMechanicalParametersSubreportData(ByVal wireCertificate As WireCertificate) As IEnumerable(Of WireCertificateMechanicalParametersReportDataRow)
        Dim mechanicalParametersSubreportData As New List(Of WireCertificateMechanicalParametersReportDataRow)
        For Each mechanicalParameter As WireCertificateMechanicalParameters In wireCertificate.MechanicalParameters
            mechanicalParametersSubreportData.Add(
                New WireCertificateMechanicalParametersReportDataRow With {
                    .Endurance = mechanicalParameter.Endurance,
                    .Percent = mechanicalParameter.Percent,
                    .Strength = mechanicalParameter.Strength,
                    .WireId = mechanicalParameter.WireId
                })
        Next
        Return mechanicalParametersSubreportData
    End Function

    Private Function GetPackagingSubreportData(ByVal wireCertificate As WireCertificate) As IEnumerable(Of WireCertificatePackagingReportDataRow)
        Dim packagingSubreportData As New List(Of WireCertificatePackagingReportDataRow)
        For Each packaging As WireCertificatePackaging In wireCertificate.Packaging
            packagingSubreportData.Add(
                New WireCertificatePackagingReportDataRow With {
                    .Count = packaging.Count,
                    .Name = packaging.Name,
                    .Weight = packaging.Weight
                })
        Next
        Return packagingSubreportData
    End Function

End Class
